package web.rec;

public class HTMLStringer {
	private StringBuilder sb;
	public HTMLStringer(){
		this.sb = new StringBuilder();
	}
	public HTMLStringer(StringBuilder sb){
		this.sb = sb;
	}
	public HTMLStringer form(){
		return new HTMLStringer(sb.append("<form >\n") );
	}
	public HTMLStringer endform(){
		return new HTMLStringer( sb.append("</form>\n") );
	}
	
	public HTMLStringer input(){
		return new HTMLStringer( sb.append("\t<input />\n") );
	}
	
	public HTMLStringer html(String val){
		return new HTMLStringer( sb.append(val) );
	}
	
	public HTMLStringer table(){
		return new HTMLStringer( sb.append("<table >\n"));
	}
	public HTMLStringer endtable(){
		return new HTMLStringer( sb.append("</table >\n"));
	}
	
	public HTMLStringer tr(){
		return new HTMLStringer( sb.append("\t<tr >\n"));
	}
	public HTMLStringer endtr(){
		return new HTMLStringer( sb.append("\t</tr >\n"));
	}

	public HTMLStringer td(){
		return new HTMLStringer( sb.append("\t<td >"));
	}
	public HTMLStringer endtd(){
		return new HTMLStringer( sb.append("</td >"));
	}
	
	public HTMLStringer attr(String attrName, String val){
		return new HTMLStringer( sb.insert(sb.length()-3, " "+  attrName + "=\""+val+"\""));
	}
	
	public String toString(){
		return sb.toString();
	}
	public StringBuilder getStringBuilder(){
		return sb;
	}
	
	public static void main(String[] args){
		String form = new HTMLStringer()
						.form().attr("id","f1")
							.input().attr("name", "in1").attr("type", "text")
						.endform()
						.toString();
		System.out.println(form);
	}
}
